package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.nf.health.app.R;
import com.nf.health.app.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class BrokenLineForSweat extends View {

	private List<Float> data;
	private int yCount;
	private float max, min, region;
	private float mWidth, mHeight;
	private float marginTop, marginBottom, marginAxis;
	private float xInterval, yInterval;
	private float margin_bottom;
	private float interval_left;
	private int goal;
	private float tb;
	private float dot_radius;
	private Paint paint_date, paint_brokenLine, paint_dot, paint_dottedline,
			framPanint, paintLine;

	private Path path;
	private float dotted_text; // 折点值

	// private Bitmap bitmap_point;

	public float getDotted_text() {
		return dotted_text;
	}

	public void setDotted_text(float dotted_text) {
		this.dotted_text = dotted_text;
	}

	private int fineLineColor = 0x5faaaaaa; // 灰色
	private int blueLineColor = 0xff00ffff; // 蓝色
	private int orangeLineColor = 0xffd56f2b; // 橙色
	private int dotColor = 0xffff0000; // 红色
	private List<String> xValues;

	public BrokenLineForSweat(Context context, List<Float> data, float max, float min,
							  final int yCount) {
		super(context);

		init(data, max, min, yCount);

	}

	private void init(List<Float> data, float max, float min, final int yCount) {
		this.data = data;
		this.max = max;
		this.min = min;
		this.region = max - min;
		this.yCount = yCount;
		marginTop = (int) getResources().getDimension(R.dimen.broken_line_margin_top);
		marginAxis = (int) getResources().getDimension(R.dimen.broken_line_margin_xaxis);
		marginBottom = (int) getResources().getDimension(R.dimen.broken_line_ymargin);
		dot_radius = (int) getResources().getDimension(R.dimen.broken_line_dot_radius);

		getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						mWidth = getWidth();
						mHeight = getHeight();
						yInterval = (mHeight - marginBottom) / yCount;
						getViewTreeObserver().removeGlobalOnLayoutListener(this);
					}
				});
		init(data);
	}
	public BrokenLineForSweat(Context context, ArrayList<String> xValues, List<Float> data, float max, float min,
							  final int yCount, int goal){
		super(context);
		this.xValues = xValues;
		this.goal = goal;
		if(goal>max){
			max = goal;
		}
		init(data, max, min, yCount);
	}


	public void init(List<Float> data) {
		if (null == data || data.size() == 0)
			return;
		this.data = data;
		Resources res = getResources();
		tb = res.getDimension(R.dimen.historyscore_tb);
		xInterval = tb * 5.0f;
		interval_left = tb * 0.5f;
		margin_bottom = 8 * tb * 0.2f;

		paint_date = new Paint();
		paint_date.setStrokeWidth(tb * 0.1f);
		paint_date.setTextSize(tb * 1f);
		paint_date.setAntiAlias(true);
		paint_date.setTextAlign(Paint.Align.CENTER);
		paint_date.setColor(Color.BLACK);

		paint_brokenLine = new Paint();
		paint_brokenLine.setStrokeWidth(tb * 0.1f);
		paint_brokenLine.setColor(getResources().getColor(R.color.theme_color));
		paint_brokenLine.setAntiAlias(true);

		paintLine = new Paint();
		paintLine.setStrokeWidth(tb * 0.1f);
		paintLine.setColor(orangeLineColor);
		paintLine.setAntiAlias(true);

		paint_dot = new Paint();
		paint_dot.setStrokeWidth(tb * 0.1f);
		paint_dot.setColor(getResources().getColor(R.color.theme_color));
		paint_dot.setAntiAlias(true);

		paint_dottedline = new Paint();
		paint_dottedline.setStyle(Paint.Style.STROKE);
		paint_dottedline.setColor(getResources().getColor(R.color.theme_color));

		framPanint = new Paint();
		framPanint.setAntiAlias(true);
		framPanint.setStrokeWidth(2f);
		framPanint.setColor(Color.TRANSPARENT);

		path = new Path();
		// bitmap_point = BitmapFactory.decodeResource(getResources(),
		// R.drawable.dot_red);
		setLayoutParams(new LayoutParams((int) (this.data.size() * xInterval),
				LayoutParams.MATCH_PARENT));
	}
	@Override
	protected void onDraw(Canvas canvas) {
		if (null == data || data.size() == 0)
			return;
		drawBrokenLine(canvas);
		drawDate(canvas);

		super.onDraw(canvas);
	}

	/**
	 * 绘制折线
	 *
	 * @param c
	 */
	public void drawBrokenLine(Canvas c) {
		float base = getHeight() - marginBottom;
		float startX = 0.0F;
		float startY = 0.0F;

		if(data.size()==1){
			float x1 = xInterval / 2;
			float y1 = base - (base * (data.get(0) - min) / region) + marginTop;
			c.drawCircle(x1, y1, dot_radius, paint_dot);
		}

		for (int i = 0; i < data.size()-1; i++) {
			float x1 = xInterval / 2 + xInterval * i;
			float y1 = base - (base * (data.get(i) - min) / region) + marginTop;
			float x2 = xInterval / 2 + xInterval * (i + 1);
			float y2 = base
					- (base * (data.get(i + 1) - min) / region) + marginTop;

			if (i == 0) {
				startX = x1;
				startY = y1;
				path.moveTo(startX, startY);
			}

			c.drawLine(x1, y1, x2, y2, paint_brokenLine);
			path.lineTo(x1, y1);
			c.drawCircle(x1, y1, dot_radius, paint_dot);
			if (i == data.size() - 2) {
				path.lineTo(x2, y2);
				path.lineTo(x2, getHeight() - marginBottom + marginTop);
				path.lineTo(startX, getHeight() - marginBottom + marginTop);
				path.close();
				c.drawPath(path, framPanint);
				c.drawCircle(x2, y2, dot_radius, paint_dot);
			}
		}

	}

	/**
	 * 绘制时间
	 *
	 * @param c
	 */
	public void drawDate(Canvas c) {
		if(goal != 0){
			for (int i = 0; i < xValues.size(); i++) {
				String text = DateUtil.date2String(Long.parseLong(xValues.get(i)), "HH:mm");
				c.drawText(text, xInterval / 2 + xInterval * i, getHeight() - marginBottom + marginAxis + marginTop, paint_date);
			}
		}else{
			for (int i = 0; i < data.size(); i++) {
				c.drawText("第" + (i + 1) + "次", xInterval / 2 + xInterval * i,
						getHeight() - marginBottom + marginAxis + marginTop, paint_date);
			}
		}
	}
}